﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace SVNClean.Modules {
    public static class Win32 {

        public const uint SHGFI_DISPLAYNAME = 0x00000200;
        public const uint SHGFI_TYPENAME = 0x400;
        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
        public const uint SHGFI_SMALLICON = 0x1; // 'Small icon

        public enum IconSize { Small = 0, Large = 1 }

        /// <summary>
        /// SHs the get file info.
        /// </summary>
        /// <param name="pszPath">The PSZ path.</param>
        /// <param name="dwFileAttributes">The dw file attributes.</param>
        /// <param name="psfi">The psfi.</param>
        /// <param name="cbSizeFileInfo">The cb size file info.</param>
        /// <param name="uFlags">The u flags.</param>
        /// <returns></returns>
        [DllImport("shell32.dll")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint
        dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);

        /// <summary>
        /// Gets the file icon.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public static Icon GetFileIcon(string path, IconSize size) {
            var hImg = IntPtr.Zero;
            var shInfo = new SHFILEINFO();

            if (size == IconSize.Small) {
                hImg = Win32.SHGetFileInfo(path, 0, ref shInfo, (uint)Marshal.SizeOf(shInfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
            }
            else {
                hImg = Win32.SHGetFileInfo(path, 0, ref shInfo, (uint)Marshal.SizeOf(shInfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
            }
            return shInfo.hIcon != IntPtr.Zero ? Icon.FromHandle(shInfo.hIcon) : null;
        }

        /// <summary>
        /// Gets the file info.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static SHFILEINFO GetFileInfo(string path) {
            var shInfo = new SHFILEINFO();
            Win32.SHGetFileInfo(path, 0, ref shInfo, (uint)Marshal.SizeOf(shInfo), Win32.SHGFI_DISPLAYNAME | Win32.SHGFI_TYPENAME);
            return shInfo;
        }

        /// <summary>
        /// API que suprime o Windows Error Report Send.
        /// </summary>
        [DllImport("faultrep.dll", CharSet = CharSet.Unicode)]
        public static extern bool AddERExcludedApplication(string name);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO {
        public IntPtr hIcon;
        public IntPtr iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    };


}
